DAY37:Find the odd int


Posted by birdbirdmurmur on 2023-08-19

題目連結

https://www.codewars.com/kata/54da5a58ea159efa38000836

解法

function findOdd(A) {
    let count = {}
    let min = Infinity

    for (const num of A) {
        count[num] ? count[num]++ : count[num] = 1
    }

    for (const num in count) {
        if (count[num] % 2 !== 0 && min > parseInt(num)) {
            min = parseInt(num)
        }
    }

    return min === Infinity ? 0 : min
}

筆記

同時練習for...offor...in兩種用法

  1. for...of用於迭代陣列等可迭代物件的
  2. for...in用於迭代物件的屬性,包括可數的屬性。

#javascript #Codewars #for...in #for...of #object







Related Posts

React-[串接api篇]-登入與驗證功能發送post ajax call

React-[串接api篇]-登入與驗證功能發送post ajax call

財務分析(2) -- 財務指標分析

財務分析(2) -- 財務指標分析

Vue.js 學習旅程Mile 9 – Event Handling 事件處理篇-1:methods & v-on

Vue.js 學習旅程Mile 9 – Event Handling 事件處理篇-1:methods & v-on


Comments